package com.recsmile.sgs.net.codec;

import com.recsmile.sgs.ServerPlayer;
import com.recsmile.sgs.core.Packet;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

/**
 * Created by hanyuanliang on 16/8/24.
 */
public class ServerChannelHandler extends SimpleChannelInboundHandler<Packet>{

    static final AttributeKey<ServerPlayer> serverPlayerAttributeKey = AttributeKey.newInstance("serverPlayers");

    protected void channelRead0(ChannelHandlerContext ctx, Packet msg) throws Exception {
        ServerPlayer serverPlayer = ctx.attr(serverPlayerAttributeKey).get();
        if(serverPlayer == null){
            ctx.attr(serverPlayerAttributeKey).set(new ServerPlayer().withCtx(ctx));
        }

        ctx.writeAndFlush(msg);
    }
}
